package com.whfc.hdc.manager.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.push.JpushProperties;
import com.whfc.common.tencent.TrtcProperties;
import com.whfc.hdc.dao.*;
import com.whfc.hdc.entity.*;
import com.whfc.hdc.manager.FshDeviceDataManager;
import com.whfc.hdc.params.FshDeviceDataParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-04-21 15:01
 */
@Service
public class FshDeviceDataManagerImpl implements FshDeviceDataManager {

    @Autowired
    private FshDeviceDataMapper fshDeviceDataMapper;

    @Autowired
    private FshDeviceDataLogMapper fshDeviceDataLogMapper;

    @Autowired
    private FshWarnMapper fshWarnMapper;

    @Autowired
    private AppDeviceMapper appDeviceMapper;

    @Autowired
    private FshDeviceConfigMapper deviceConfigMapper;

    @NacosValue("${fsh.default.request.host}")
    private String requestHost;

    @Autowired
    private TrtcProperties trtcProperties;

    @Autowired
    private JpushProperties jpushProperties;

    @Override
    public void initDeviceConfig(String sn) {
        AppDevice appDevice = appDeviceMapper.selectBySn(sn);
        //设备不存在或者设备类型不为 视频安全帽则跳过
        if (appDevice == null || !DeviceType.videoHelmet.value().equals(appDevice.getType())) {
            return;
        }
        FshDeviceConfig deviceConfig = deviceConfigMapper.selectBySn(sn);
        //已存在该设备，跳过初始化
        if (deviceConfig != null) {
            return;
        }

        FshDeviceConfig fshDeviceConfig = new FshDeviceConfig();
        fshDeviceConfig.setDeviceId(appDevice.getId());
        fshDeviceConfig.setSn(appDevice.getCode());
        fshDeviceConfig.setRequestHost(requestHost);
        fshDeviceConfig.setTcAppId(String.valueOf(trtcProperties.getAppId()));
        fshDeviceConfig.setJpushAppKey(jpushProperties.getAppKey());
        fshDeviceConfig.setJpushSecret(jpushProperties.getMasterSecret());
        deviceConfigMapper.insertSelective(fshDeviceConfig);
    }

    @Override
    public void addDeviceData(FshDeviceDataParam param) {
        if (param == null) {
            return;
        }
        String sn = param.getSn();
        FshDeviceData fshDeviceData = fshDeviceDataMapper.selectBySn(sn);
        String alarms = null;
        if (param.getAlarms() != null && !param.getAlarms().isEmpty()) {
            alarms = JSON.toJSONString(param.getAlarms());
        }
        if (fshDeviceData == null) {
            fshDeviceData = new FshDeviceData();
            BeanUtils.copyProperties(param, fshDeviceData);
            fshDeviceData.setAlarms(alarms);
            fshDeviceDataMapper.insertSelective(fshDeviceData);
        } else {
            BeanUtils.copyProperties(param, fshDeviceData);
            fshDeviceData.setAlarms(alarms);
            fshDeviceDataMapper.updateByPrimaryKeySelective(fshDeviceData);
        }
        //保存设备记录
        FshDeviceDataLog fshDeviceDataLog = new FshDeviceDataLog();
        BeanUtils.copyProperties(param, fshDeviceDataLog);
        fshDeviceDataLog.setAlarms(alarms);
        fshDeviceDataLogMapper.insertSelective(fshDeviceDataLog);

        //保存报警记录
        if (StringUtils.isNotBlank(alarms)) {
            saveWarn(param, fshDeviceDataLog.getId());
        }

    }

    /**
     * 保存报警记录
     *
     * @param param 设备数据
     */
    private void saveWarn(FshDeviceDataParam param, Integer dataLogId) {
        List<FshWarn> warns = new ArrayList<>();
        List<Integer> alarms = param.getAlarms();
        for (Integer alarm : alarms) {
            FshWarn fshWarn = new FshWarn();
            fshWarn.setDataLogId(dataLogId);
            fshWarn.setDeviceId(param.getDeviceId());
            fshWarn.setSn(param.getSn());
            fshWarn.setTime(param.getTime());
            fshWarn.setRuleType(alarm);
            warns.add(fshWarn);
        }
        fshWarnMapper.batchInsert(warns);
    }
}
